#-*-coding:utf-8-*-
'''
Created on 2011-2-6

@author: zhaoyh
'''
from sqlalchemy import Column, Integer,String
from db.meta import Base,metadata,DBSession
class Settings(Base):
    '''设置
    __tablename__ = 'ad_settings'
    set_id = Column(Integer(10), primary_key=True)
    set_key = Column(String(100),nullable=False )
    set_value = Column(String(100),nullable=False )
    set_notes=Column(String(100))
    '''
    __tablename__ = 'ad_settings'
    set_id = Column(Integer(10), primary_key=True)
    set_key = Column(String(100),nullable=False )
    set_value = Column(String(100),nullable=False )
    set_notes=Column(String(100))
    def __init__(self, **kwargs ):
		for k in kwargs.keys():
			setattr(self, k, kwargs[k])  
    def __repr__(self):
        return "<Settings('%s','%s','%s' )>" % (self.set_key, self.set_value,self.set_notes) 
def get_setting(key):
    """
    @param key:设置代码
    @return: vaule 
    """
    set=DBSession.query(Settings).filter(Settings.set_key==key).first()
    if set:
        return set.set_value
    else:
        return None
class Company(Base):
    '''单位信息
    __tablename__ = 'ad_company'
    co_id = Column(String(100), primary_key=True)
    co_code = Column(String(100),nullable=False )
    co_name = Column(String(100),nullable=False )
    co_address = Column(String(100))
    co_manager = Column(String(100))
    co_phone = Column(String(50))
    co_website=Column(String(50))
    co_email=Column(String(50))
    co_notes=Column(String(500)
    '''
    __tablename__ = 'ad_company'
    columntype={'id':'co_id','code':'co_code'}
    co_id = Column(Integer(10), primary_key=True)
    co_code = Column(String(100),nullable=False )
    co_name = Column(String(100),nullable=False )
    co_address = Column(String(100))
    co_manager = Column(String(100))
    co_phone = Column(String(50))
    co_website=Column(String(50))
    co_email=Column(String(50))
    co_notes=Column(String(500))
    def __init__(self, **kwargs ):
		for k in kwargs.keys():
			setattr(self, k, kwargs[k])
        
    def __repr__(self):
        return "<Company('%s','%s', )>" % (self.co_code, self.co_name)    
department_table=Table('ad_department',metadata,
                        Column('dept_id',Integer(10),primary_key=True),
                        Column('dept_code',String(100),nullable=False),
                        Column('dept_name',String(100),nullable=False),
                        Column('dept_fullname',String(200)),
                        Column('parent_id',Integer(10)),
                        Column('is_detail',String(1)),
                        Column('levels',Integer(10)),
                        Column('is_active',String(1)),
                        Column('dept_notes',String(500))
                       )   
class Department(object):
    '''部门
    __tablename__='ad_department'
    dept_id=Column(Integer(10),primary_key=True)
    dept_code=Column(String(100),nullable=False)
    dept_name=Column(String(100),nullable=False)
    parent_id=Column(Integer(10))
    is_detail=Column(String(1))
    level=Column(Integer(10))
    is_active=Column(String(1))
    dept_notes=Column(String(500))
    parent=relationship('ad_department'    '''
    query = DBSession.query_property()
    columntype={'id':'dept_id','code':'dept_code'}
    def __init__(self, **kwargs ):
		for k in kwargs.keys():
			setattr(self, k, kwargs[k])
    def __repr__(self):
        return"<Department('%s','%s')>" %(self.dept_code,self.dept_name)
mapper(Department,department_table)

class EmployeeType(Base):
    '''职员类型
    __tablename__='ad_EmployeeType'
    et_id=Column(Integer(10),primary_key=True)
    et_code=Column(String(100),nullable=False)
    et_name=Column(String(100),nullable=False)
    is_active=Column(String(1))
    et_notes=Column(String(500))
    '''
    __tablename__='ad_EmployeeType'
    et_id=Column(Integer(10),primary_key=True)
    et_code=Column(String(100),nullable=False)
    et_name=Column(String(100),nullable=False)
    is_active=Column(String(1))
    et_notes=Column(String(500))
    #employee=relationship("Child", backref=backref("parent", uselist=False))
    def __init__(self, **kwargs ):
		for k in kwargs.keys():
			setattr(self, k, kwargs[k])
    def __repr__(self):
        return"<EmployeeType('%s','%s')>" %(self.et_code,self.et_name)
class Employee(Base):
    '''职员
    __tablename__='ad_employee'
    ee_id=Column(Integer(10),primary_key=True) id
    ee_code=Column(String(100),nullable=False)代码
    order_code=Column(String(50)) 排序号
    ee_name=Column(String(100),nullable=False)姓名
    dept_id=Column(Integer(10),ForeignKey("ad_department.dept_id"))部门
    is_user=Column(String(1))是否登陆
    user_id=Column(String(50))登陆名
    ee_birthdate=Column(Date(50))生日
    et_id=Column(Integer(10))类别
    ee_sex=Column(String(1)) 性别
    ee_email=Column(String(50))email
    ee_im=Column(String(50)) IM号
    ee_address=Column(String(100)) 地址
    ee_telphone=Column(String(50))电话
    ee_edu=Column(String(50)) 文化程度
    ee_mobile=Column(String(50))移动电话
    is_active=Column(String(1))启用
    ee_notes=Column(String(500))备注
    '''
    __tablename__='ad_employee'
    ee_id=Column(Integer(10),primary_key=True ,info={"visible": False})
    ee_code=Column(String(100),nullable=False)
    order_code=Column(String(50))
    ee_name=Column(String(100),nullable=False)
    dept_id=Column(Integer(10),ForeignKey("ad_department.dept_id"))
    is_user=Column(String(1))
    user_id=Column(String(50))
    ee_birthdate=Column(Date(50))
    et_id=Column(Integer(10),ForeignKey("ad_EmployeeType.et_id"))
    ee_sex=Column(String(1))
    ee_email=Column(String(50))
    ee_im=Column(String(50))
    ee_address=Column(String(100))
    ee_telphone=Column(String(50))
    ee_edu=Column(String(50))
    ee_mobile=Column(String(50))
    is_active=Column(String(1))
    ee_notes=Column(String(500))
    employeetype=relationship(EmployeeType,  uselist=False)
    dept=relationship(Department,  uselist=False)
    def __init__(self, **kwargs ):
		for k in kwargs.keys():
			setattr(self, k, kwargs[k])
    def __repr__(self):
        return"<Employee('%s','%s','%s')>" %(self.ee_id,self.ee_code,self.ee_name) 

user_table=Table('ad_user',metadata,
            Column('user_id',String(100), primary_key=True),
            Column('user_name',String(100),nullable=False ),
            Column('user_pwd',String(100)),
            Column('user_email',String(100)),
            Column('is_lock',String(1)),
            Column('group_id',Integer(10),ForeignKey("ad_group.group_id"))
            )
class User(object):
    '''用户
        self.user_id = user_id
        self.user_name = user_name
        self.user_pwd = user_pwd
        self.user_email=user_email
        self.is_lock=is_lock
        group_id
    '''
    columntype={'id':'user_id','code':'user_id'}
    query = DBSession.query_property()
    #__tablename__ = 'ad_user'
       def __init__(self, **kwargs ):
       		for k in kwargs.keys():
       			setattr(self, k, kwargs[k])
       
    def __repr__(self):
        return "<User(user_id='%s','%s', '%s', '%s', '%s')>" % (self.user_id, self.user_name, self.user_pwd,self.user_email,self.is_lock)    
mapper(User, user_table, order_by=user_table.c.user_id)   
class Group(Base):
    '''用户组
    __tablename__='ad_group'
    group_id=Column(Integer(10),primary_key=True)
    group_code=Column(String(100),nullable=False)
    group_name=Column(String(100))
    '''
    __tablename__='ad_group'
    columntype={'id':'group_id','code':'group_code'}
    group_id=Column(Integer(10),primary_key=True)
    group_code=Column(String(100),nullable=False)
    group_name=Column(String(100))
    note=Column(String(100))
    def __init__(self, **kwargs ):
    		for k in kwargs.keys():
    			setattr(self, k, kwargs[k])
    
    def __repr__(self):        
        return"<Group('%s','%s','%s')>" %(self.group_id,self.group_code,self.group_name)
class Permissions(Base):
    '''权限设置
    __tablename__='ad_permissions'
    permissions_id=Column(Integer(10),primary_key=True)
    permissions_name=Column(String(100))
    operate_id=Column(Integer(10),ForeignKey("ad_operate.operate_id"))'''
    __tablename__='ad_permissions'
    permissions_id=Column(Integer(10),primary_key=True)
    permissions_name=Column(String(100))
    operate_id=Column(Integer(10),ForeignKey("ad_operate.operate_id"))
    def __init__(self, **kwargs ):
		for k in kwargs.keys():
			setattr(self, k, kwargs[k])
    def __repr__(self):
        return"<Permissions('%s','%s')>" %(self.permissions_id,self.permissions_name) 
class Operate(Base):
    '''权限的操作表，基础表，查询，增加，修改，删除等    
    __tablename__='ad_operate'
    operate_id=Column(Integer(10),primary_key=True)
    operate_name=Column(String(100))
    '''
    __tablename__='ad_operate'
    operate_id=Column(Integer(10),primary_key=True)
    operate_name=Column(String(100))
    def __init__(self, **kwargs ):
		for k in kwargs.keys():
			setattr(self, k, kwargs[k])
    def __repr__(self):
        return "<Operate('%s','%s')>" %(self.operate_id,self.operate_name) 

if __name__ == "__main__":
    metadata.create_all() 
